package cn.itsource.config;

import cn.hutool.core.util.IdUtil;
import cn.itsource.constants.RConst;
import cn.itsource.mapper.LoginMapper;
import cn.itsource.pojo.domain.Login;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.commons.util.IdUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

@Component
public class BootstrapListener  implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private LoginMapper loginMapper;

	@Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        Login login = new Login();
        login.setId(IdUtil.createSnowflake(RConst.Num.INT_1,RConst.Num.INT_1).nextId());
        login.setUsername("admin");
        //  必须加密啊
        login.setPassword(passwordEncoder.encode("123456"));
        login.setType(RConst.Num.INT_0);
        login.setEnabled(true);
        login.setAvatar("https://img2.baidu.com/it/u=3118662302,494940350&fm=253&app=138&f=JPEG?w=800&h=974");
        login.setAdmin(true);
        login.setNickName("admin");
        login.setName("admin");
        Login login1 = loginMapper.selectOne(new LambdaQueryWrapper<Login>()
                .eq(Login::getUsername, login.getUsername())
                .eq(Login::getType, RConst.Num.INT_0)
                .eq(Login::getAdmin, true)
        );
        if(login1 == null){
            loginMapper.insert(login);
        }
    }
}